﻿using System;
using System.Collections.Generic;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eFirmwares : EditBase
    {

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {
             BindGridItems();
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            rgListItems.ItemCommand += new GridCommandEventHandler(rgListItems_ItemCommand);
        }

        #endregion

        #region Event Handlers

        protected void rgListItems_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "EditItem")
            {
                Guid userId = new Guid(rgListItems.Items[e.Item.ItemIndex].GetDataKeyValue("FirmwareId").ToString());
                RaiseNavEvent(this, new NavEventArgs("eFirmware", userId));
            }
        }

        #endregion


        public void BindGridItems()
        {
            FirmwareController fc = new FirmwareController();
            List<FirmwareInfo> fiList = fc.SelectFirmwares("Version Asc");

            ModelController mc = new ModelController();

            // get model name
            foreach (FirmwareInfo fi in fiList)
            {
                if (fi.ModelId!=Guid.Empty)
                {
                    ModelInfo mi = mc.SelectModel(fi.ModelId);
                    if (mi!=null)
                        fi.ModelName = mi.Name;
                }
                
            }
           
            rgListItems.DataSource = fiList;
            rgListItems.DataBind();


        }


        #region Action Events
        public void DeleteItem(Guid id)
        {
            FirmwareController fc = new FirmwareController();
            fc.DeleteFirmware(id);
            RaiseNavEvent(this, new NavEventArgs("eFirmwares", id));
        }


        #endregion


        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Add":
                    RaiseNavEvent(this, new NavEventArgs("eFirmware", new Guid()));                        
                    break;
                case "Edit":
                    if (rgListItems.SelectedValue != null)
                    {
                        RaiseNavEvent(this, new NavEventArgs("eFirmware", new Guid(rgListItems.SelectedValue.ToString())));                        
                    }
                    break;
                case "Delete":
                    if (rgListItems.SelectedValue != null)
                    {
                        DeleteItem(new Guid(rgListItems.SelectedValue.ToString()));                        
                    }
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get 
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Add", "Add Firmware", "~/images/icons/add.png"));
                al.Add(new ActionInfo("Edit", "Edit Firmware", "~/images/icons/edit.png"));
                al.Add(new ActionInfo("Delete", "Delete Firmware", "~/images/icons/delete.png", "Are you sure you want to delete this firmware?", true));
                return al;
            }
        }

        #endregion

    }
}